#pragma once

#include "Widget/QRSWidgetBase.h"
#include "ui_QFillOperationWidget.h"

#include "Render/QIVtkSelectionPipeline.h"
#include <TopoDS.hxx>
#include <TopoDS_Wire.hxx>
#include <TopoDS_Edge.hxx>
#include <set>

QT_BEGIN_NAMESPACE
namespace Ui { class QFillOperationWidgetClass; };
QT_END_NAMESPACE

class QFillOperationWidget : public QRSWidgetBase
{
	Q_OBJECT

public:
	QFillOperationWidget(MainWindow* inMainWindow, ShapeOperation* inOperation = nullptr, QWidget* parent = nullptr);
	~QFillOperationWidget();

	// 将一组edge转化为wire
	static std::vector<TopoDS_Wire> BuildWiresFromEdges(const std::vector<TopoDS_Edge>& edges);
	// 将wire组合形成face，外圈形成基本面，内圈形成孔
	static TopoDS_Face CreateFaceWithHoles(std::vector<TopoDS_Wire>& wires);

protected:
	void OnSelectedSubShapes(QIVtkSelectionPipeline* actor);
	virtual void closeEvent(QCloseEvent* event) override;

	void OnModify();
	void OnConfirm();

	void FillFace();

	// 处理边，提取出闭合edge转化为wire
	static std::tuple<std::vector<TopoDS_Wire>, std::vector<TopoDS_Edge>> PreprocessEdges(const std::vector<TopoDS_Edge>& edges);
	// 找到最外层wire
	static TopoDS_Wire FindOuterWire(std::vector<TopoDS_Wire>& wires);
	// 判断线框 inner 是否在 outer 内部：
	static bool IsWireInside(const TopoDS_Wire& outer, const TopoDS_Wire& inner);// 可能存在问题
	// 判断wire是否为顺时针
	static bool IsWireClockwise(const TopoDS_Wire& wire);
	// 修复wire的方向
	static void FixWireOrientation(TopoDS_Wire& wire, bool isOuter);
	// 求闭合wire包围的面积
    static double GetWireArea(const TopoDS_Wire& wire);

private:
	Ui::QFillOperationWidgetClass *ui;

	std::vector<TopoDS_Edge> selectEdges;
	std::set<QIVtkSelectionPipeline*> selectActors;
};
